package com.hetang.maijiayun.adaptertools.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hetang.maijiayun.adaptertools.b2cinterface.paipai.client.PaipaiClient;
import com.hetang.maijiayun.adaptertools.b2cinterface.paipai.domain.Deal;
import com.hetang.maijiayun.adaptertools.b2cinterface.paipai.request.SellerSearchDealListRequest;
import com.hetang.maijiayun.adaptertools.b2cinterface.paipai.response.SellerSearchDealListResponse;
import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;

public class PaipaiAdapter extends AdapterAbstract {
	 /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(PaipaiAdapter.class);

    @Override
    public Integer request(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        return null;
    }

    @Override
    public List<String> requestOrders(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        if (MapUtils.isEmpty(authorizesMap) || !authorizesMap.containsKey("url") || !authorizesMap.containsKey("appKey") || !authorizesMap.containsKey("secret") || !authorizesMap.containsKey("sessionKey") || !authorizesMap.containsKey("uin")) {
            logger.error("该拍拍平台店铺没有授权信息或授权信息错误！！");
            return null;
        }
        PaipaiClient client = new PaipaiClient(authorizesMap.get("url"), authorizesMap.get("appKey"), authorizesMap.get("secret"), authorizesMap.get("sessionKey"), authorizesMap.get("uin"));
        SellerSearchDealListRequest req = new SellerSearchDealListRequest();
        req.setSellerUin(Long.getLong(authorizesMap.get("uin")));
        req.setTimeType("UPDATE");
        req.setTimeBegin(request.getStartTime());
        req.setTimeEnd(request.getEndTime());
        req.setPageIndex(1L);
        req.setPageSize(20L);
        logger.info("paipai get order interface, request time is : {}, request param is : {}", System.currentTimeMillis(), JsonTools.createJsonString(req));
        SellerSearchDealListResponse rsp = client.execute(req);
        logger.info("paipai get order interface, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
        if (rsp == null || !"".equals(rsp.getErrorCode())) {
            return null;
        }
        Long page = 0L;
        Long mod = rsp.getCountTotal()%20L;
        page = rsp.getCountTotal()/20L;
        if (0 != mod) {
            page += 1;
        }
        List<String> result = new ArrayList<String>();
        while (page > 0) {
            req.setPageIndex(page);
            rsp = client.execute(req);
            if (rsp.getErrorCode() != 0) {
                logger.error("paipai get order interface error, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
                continue;
            }
            if (CollectionUtils.isEmpty(rsp.getDealList())) {
                continue;
            }
            for (Deal deal : rsp.getDealList()) {
                result.add(deal.getDealCode() + "");
            }
            page -- ;
        }
        return result;
    }
}
